import pandas as pd

from .. import StraTmpl
from ..indicator.hl_relationship import *


class StraPVC(StraTmpl):
    """基于峰－谷－循环的策略
    峰－谷－循环: 见 ..indicator.hl_relationship.py
    利用tick数据
    基于时间窗口 windows 和 变化百分比 rng确定的峰和谷,以及其间的循环关系

    """
    windows = 600
    rng = 5  # 千分比
    pv = pd.DataFrame()
    stat = pd.DataFrame()

    def on_tick(self, tick):
        """
                pv = pv_new_tick(pv, price, self.data_len, self.rng, self.windows)
                columns=['f_4', 'f_3', 'f_2', 'f_1', 'flag', 'pv4','g_2',
    'short', 'money', 'long', 'count', 'total_prof', 'short_mean', 'money_mean', 'long_mean','udr',
    'price','posi','ok_p','ok_posi','ok_p_5','ok_posi_5','ok_p_4','ok_posi_4','ok_p_3','ok_posi_3',
    'ok_p_2','ok_posi_2','ok_p_1','ok_posi_1','diff_ok_p4','diff_ok_p3','diff_ok_p2','diff_ok_p1'
       ]
       stat:  short money long s_m m_m l_m
              s_w_r m_w_r l_w_r s_c m_c l_c t_prof
              short, s_m

        """
        price = tick.LastPrice
        pv = self.pv
        self.data_len += 1
        lst_pv = pv.iloc[-1]
        cur_pv_flag, p0, v0, pp0, vp0, xprice, xposi = lst_pv[['flag', 'hh', 'll', 'hhp', 'llp', 'price', 'posi']]
        rslt = clc_pv_tick(p0, v0, price, pp0, vp0, self.data_len, cur_pv_flag, xprice, xposi, self.rng, self.windows)
        p, v, pp, vp, pv_flag, xprice, xposi = rslt
        if cur_pv_flag != pv_flag:

            f_4, f_3, f_2, f_1 = lst_pv[['f_3', 'f_2', 'f_1', 'flag']]
            p_4, p_3, p_2, p_1 = lst_pv[['price_3', 'price_2', 'price_1', 'price']]
            pv4 = clc_zc(p_3, p_2, p_1, xprice)
            # print(f'{pv4}:pre_pv4=={lst_pv.pv4} and flag=={pv_flag},{pv.tail(5)}')
            print(f'data len:{self.data_len},f_3=={f_3} and flag=={pv_flag} and f_2=={f_2} and f_1=={f_1}')
            s = self.stat.query(f'f_3=={f_3} and flag=={pv_flag} and f_2=={f_2} and f_1=={f_1}')
            # print(s)
            if not s.empty:  # (f_4, f_3, f_2, f_1, pv_flag) in self.stat.index:
                # s = self.stat.loc[(f_4, f_3, f_2, f_1, pv_flag), :]
                s = pd.Series(s.iloc[0])
                print(s)
                if s.t_prof > 0:
                    bs = 1
                    st_p = s.s_m
                    self.send_signal(bs, price, 1, abs(s.long / s.t_prof))
                    if st_p < 0:
                        self.send_stop_signal(price + st_p, bs)
                elif s.t_prof < 0:
                    bs = -1
                    st_p = s.l_m
                    self.send_signal(bs, price, 1, abs(s.short / s.t_prof))
                    if st_p > 0:
                        self.send_stop_signal(price + st_p, bs)

            new = {'flag': pv_flag, 'price': xprice, 'posi': xposi, 'ok_p': price,
                   'ok_posi': self.data_len, 'hh': p, 'll': v, 'hhp': pp, 'llp': vp,
                   'f_4': lst_pv.f_3, 'f_3': lst_pv.f_2, 'f_2': lst_pv.f_1, 'f_1': lst_pv.flag,
                   'pv4': pv4, 'pre_pv4': lst_pv.pv4}
            pv = pv.append(new, ignore_index=True)
            # 不调用 set_flag and classifier_stat, 仅更新需要的可以节省时间
            # 更新标志
            # pv = set_flag(pv)
            # stat = classifier_stat(pv)
            # self.stat = stat
            self.pv = pv
            # print(pv4, self.data_len)
        else:  # 没有新的峰谷被确认
            idx = pv.tail(1).index[0]
            pv.loc[idx, ['hh', 'll', 'hhp', 'llp']] = [p, v, pp, vp]
            # return pv, rdf, prob, bs, position, stop_price
            self.pv = pv

    def on_ticks(self, ticks):
        """"""
        i = 0
        last_prices = ticks.LastPrice
        pv = pv_tick(last_prices, self.rng, self.windows)
        # price = ticks.iloc[0].LastPrice
        #
        # pv = pd.DataFrame({'flag': 0, 'hh': price, 'll': price, 'hhp': 0, 'llp': 0, 'price': price, 'posi': 0},
        #                   index=[0])
        # self.data_len = len(last_prices)
        # for i in range(1, self.data_len):
        #     pv = pv_new_tick(pv, last_prices[i], i, self.rng, self.windows)
        pv = set_flag(pv)
        self.pv = pv
        self.stat = classifier_stat(pv, ['f_3', 'f_2', 'f_1', 'flag'])
        self.data_len = len(ticks)

    def set_stop_order(self, stop_order):
        """"""

    def save_params(self, params=None):
        params = {'rng': self.rng, 'windows': self.windows, }
